﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using EmptyWallGallery.Core;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.Core.BLL
{
    public class SlideShowListView
    {
        public int SlideId { get; set; }
        public string ImageDesc { get; set; }
        public string ImageUrl { get; set; }
        public bool? Status { get; set; }
        public string CreateBy { get; set; }
        public DateTime? CreateDate { get; set; }
    }

    public class SlideShow
    {
        public static SlideShow Instance
        {
            get { return new SlideShow(); }
        }

        public static SlideShowListView LoadDataFromListItem(Core.SlideShow item)
        {
            var objSlide = new SlideShowListView
            {
                CreateBy = item.CreateBy.ToString(),
                CreateDate = item.CreateDate,
                ImageDesc = item.ImageDesc,
                ImageUrl = item.ImageUrl,
                SlideId = item.SlideId,
                Status = item.Status
            };

            return objSlide;
        }

        /// <summary>
        /// Create Slide Show
        /// </summary>
        /// <param name="imageDesc">Image Description</param>
        /// <param name="imageUrl">Image Url</param>
        /// <param name="status">Slide Status</param>
        /// <param name="createBy">Create By</param>
        /// <returns></returns>
        public bool Create(string imageDesc, string imageUrl, bool status, string createBy)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objSlide = new Core.SlideShow
                                       {
                                           CreateBy = new Guid(createBy),
                                           CreateDate = DateTime.Now,
                                           ImageDesc = Common.Instance.Truncate(imageDesc, 350, false, false),
                                           ImageUrl = imageUrl,
                                           Status = status
                                       };

                    dc.SlideShows.InsertOnSubmit(objSlide);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Update Slide Show
        /// </summary>
        /// <param name="imageDesc">Image Description</param>
        /// <param name="imageUrl">Image Url</param>
        /// <param name="status">Image Status</param>
        /// <param name="createBy">Create By</param>
        /// <param name="slideId">Slide Id</param>
        /// <returns></returns>
        public bool Update(string imageDesc, string imageUrl, bool status, string createBy, int slideId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objSlide = (dc.SlideShows.Where(p => p.SlideId == slideId)).Single();
                    objSlide.ImageDesc = Common.Instance.Truncate(imageDesc, 350, false, false);
                    objSlide.ImageUrl = imageUrl;
                    objSlide.Status = status;
                    //objSlide.CreateBy = Guid.Parse(createBy);

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Update Status
        /// </summary>
        /// <param name="slideId">Slide Id</param>
        /// <param name="status">Slide Status</param>
        /// <returns></returns>
        public bool UpdateStatus(int slideId, bool status)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objSlide = (dc.SlideShows.Where(p => p.SlideId == slideId)).Single();
                    objSlide.Status = status;

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Delete Slide
        /// </summary>
        /// <param name="slideId">Slide Id</param>
        /// <returns></returns>
        public bool Delete(int slideId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objSlide = (dc.SlideShows.Where(p => p.SlideId == slideId)).Single();

                    var strPath = Configs.UserFolder + objSlide.ImageUrl.Replace("/", @"\");
                    if (Common.Instance.DeleteFile(strPath))
                    {
                        dc.SlideShows.DeleteOnSubmit(objSlide);
                        dc.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Get All Slide
        /// </summary>
        /// <param name="status">Slide Status</param>
        /// <returns></returns>
        public List<SlideShowListView> GetAllSlide(int status)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = from slideShow in dc.SlideShows
                            where (status == 2 || (slideShow.Status == Convert.ToBoolean(status)))
                            orderby slideShow.CreateDate descending 
                            select new SlideShowListView
                                       {
                                           CreateDate = slideShow.CreateDate,
                                           Status = slideShow.Status,
                                           CreateBy = slideShow.CreateBy.ToString(),
                                           ImageDesc = slideShow.ImageDesc,
                                           SlideId = slideShow.SlideId,
                                           ImageUrl = slideShow.ImageUrl
                                       };

                return query.ToList();
            }
        }

        /// <summary>
        /// Get Slide Detail
        /// </summary>
        /// <param name="slideId">Slide Id</param>
        /// <returns></returns>
        public SlideShowListView GetById(int slideId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = (from s in dc.SlideShows
                             where s.SlideId == slideId
                             select s).SingleOrDefault();

                return LoadDataFromListItem(query);
            }
        }

        /// <summary>
        /// Write Slide to Json file.
        /// </summary>
        /// <returns></returns>
        public bool WriteSlideItem()
        {
            try
            {
                using (var sWriter = new StreamWriter(Configs.FolderSaveData + "slide.js", false, Encoding.UTF8))
                {
                    var js = new JavaScriptSerializer();
                    sWriter.Write(js.Serialize(GetAllSlide(1)));
                    sWriter.Flush();
                    sWriter.Close();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }  
        }
    }
}
